package com.dura.smsgateway.mq;

import com.dura.common.model.StandardSubmit;
import com.dura.common.util.CMPPSubmitRepoMapUtil;
import com.dura.smsgateway.netty4.NettyClient;
import com.dura.smsgateway.netty4.entity.CmppSubmit;
import com.dura.smsgateway.netty4.utils.Command;
import com.dura.smsgateway.netty4.utils.MsgUtils;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @version 1.0
 * @Author 振鹏
 * @Date 2025/3/19 10:07
 * @注释
 */
@Component
@Slf4j
public class SmsGatewayListener {

    @Autowired
    private NettyClient nettyClient;

    @RabbitListener(queues = "${gateway.sendtopic}")
    public void consume(StandardSubmit submit, Channel channel, Message message) throws IOException {

        log.info("【短信网关模块】 接收到消息，submit={}",submit);

        // 完成与运营商的交互，发送一次请求，接收两次响应
        // 1、获取核心属性
        String srcNumber = submit.getSrcNumber();
        String mobile = submit.getMobile();
        String text = submit.getText();

        int sequence = MsgUtils.getSequence();

        CmppSubmit cmppSubmit=new CmppSubmit(Command.CMPP2_VERSION,srcNumber,sequence,mobile,text);

        // 2、声明发送短信的时候，需要的对象

        // 3、将submit对象做一个临时存储，在运营商第一次响应的时候获取到
         CMPPSubmitRepoMapUtil.put(sequence,submit);
        //4、和运营商交互发送短信
        nettyClient.submit(cmppSubmit);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }

}
